<template>
  <div>
    <el-dropdown trigger="click" @command="handleSetLangurage">
      <div v-if="showTitle" class="light">{{ title }}</div>
      <div v-else class="language-icon--style">
        <svg-icon class-name="language-icon" icon-class="language" />
      </div>

      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item
            v-for="item of languageOptions"
            :key="item.value"
            :disabled="language === item.value"
            :command="item.value"
          >
            {{ item.label }}
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import useAppStore from "@/store/modules/app";

const appStore = useAppStore();
const language = computed(() => appStore.language);
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const props = defineProps({
  title: {
    type: String,
    default: "",
  },
});
const showTitle = computed(() => props.title.trim() !== "");
const languageOptions = ref([
  { label: t("common.chinese"), value: "zh" },
  { label: t("common.english"), value: "en" },
]);

function handleSetLangurage(language) {
  proxy.$modal.loading(t("common.setLangTip"));
  appStore.setLanguage(language);
  setTimeout("window.location.reload()", 1000);
}
</script>

<style lang="scss" scoped>
.language-icon--style,
.light {
  font-size: 18px;
  line-height: 50px;
  padding-right: 7px;
}
.light {
  cursor: pointer;
  color: #fff !important;
}
</style>
